﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LandandSurvey.BLL;

namespace LandandSurvey
{
    public partial class SearchBookPlan : System.Web.UI.Page
    {
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                Manager manager = new Manager();
                ddlSurveyor.DataSource = manager.GetSurveyors( false );
                ddlSurveyor.DataTextField = "Name";
                ddlSurveyor.DataBind();

                ddlWard.DataSource = manager.GetCountys( false );
                ddlWard.DataTextField = "Ward";
                ddlWard.DataBind();
            }
        }

        protected void btnSearch_Click( object sender, EventArgs e )
        {
            gvResults.DataSource = Search();
            gvResults.DataBind();
        }

        private List<BookPlan> Search()
        {
            landsdbEntities context = new landsdbEntities();

            var result = from i in context.BookPlans
                         select i;

            if ( txtCTFolio.Text != string.Empty )
            {
                result = from i in result
                         where i.CTFolio == txtCTFolio.Text
                         select i;
            }

            if ( txtAcreage.Text != string.Empty )
            {
                result = from i in result
                         where i.Acreage == txtAcreage.Text
                         select i;
            }

            if ( txtCTVolume.Text != string.Empty )
            {
                result = from i in result
                         where i.CTVolume == txtCTVolume.Text
                         select i;
            }

            if ( txtDateSignedDirector.Text != string.Empty )
            {
                result = from i in result
                         where i.DateSignedDirector == txtDateSignedDirector.Text
                         select i;
            }

            if ( txtDateSignedSurveyor.Text != string.Empty )
            {
                result = from i in result
                         where i.DateSignedSurveyor == txtDateSignedSurveyor.Text
                         select i;
            }

            if ( txtLSNumber.Text != string.Empty )
            {
                result = from i in result
                         where i.LandSNumber == txtLSNumber.Text
                         select i;
            }

            if ( txtName.Text != string.Empty )
            {
                result = from i in result
                         where i.Name == txtName.Text
                         select i;
            }

            if ( txtBookNo.Text != string.Empty )
            {
                result = from i in result
                         where i.BookNo == txtBookNo.Text
                         select i;
            }

            if ( txtFolio.Text != string.Empty )
            {
                result = from i in result
                         where i.Folio == txtFolio.Text
                         select i;
            }

            if ( txtRPOFolio.Text != string.Empty )
            {
                result = from i in result
                         where i.RPOFolio == txtRPOFolio.Text
                         select i;
            }

            if ( txtRPOVolume.Text != string.Empty )
            {
                result = from i in result
                         where i.RPOVolume == txtRPOVolume.Text
                         select i;
            }

            if ( txtSurveoyOrderNo.Text != string.Empty )
            {
                result = from i in result
                         where i.SurveyOrderNumber == txtSurveoyOrderNo.Text
                         select i;
            }

            if ( ddlSurveyor.Text != string.Empty )
            {
                result = from i in result
                         where i.Surveyor == ddlSurveyor.Text
                         select i;
            }

            if ( ddlWard.Text != string.Empty )
            {
                result = from i in result
                         where i.Ward == ddlWard.Text
                         select i;
            }

            if ( txtWardRefCassini.Text != string.Empty )
            {
                result = from i in result
                         where i.WardRefCassini == txtWardRefCassini.Text
                         select i;
            }

            if ( txtWardRefUTM.Text != string.Empty )
            {
                result = from i in result
                         where i.WardRefUTM == txtWardRefUTM.Text
                         select i;
            }

            if ( txtDeedNumber.Text != string.Empty )
            {
                result = from i in result
                         where i.DeedNumber == txtDeedNumber.Text
                         select i;
            }

            return result.ToList();
        }
    }
}